//
//  AppDelegate.swift
//  swift-weibo
//
//  Created by 陈乾 on 15/10/26.
//  Copyright © 2015年 陈乾. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    
    var window: UIWindow?
    
    
    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        
        //渲染
        appreance()
        //创建窗口
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        
        window?.rootViewController = loginXXOO()
            
        //？：如果window为空就不执行后面的代码
        window?.makeKeyAndVisible()
        
        return true
    }
    
    
    //MARK: - 登录逻辑
    func loginXXOO() ->UIViewController {
        //如果用户没有登录
        if !CQUserAccount.loginYN {
            //去tabVC tabVC默认显示了Homeview 。在去BsacVC 里面判断登录没有 如果登陆了就是首页的tabelView 否者就是访客视图
            return CQTabBarController()
        }
        else {
        
          //根据是否是新版本 版本相同 返回yes我们就去新特性界面
            return isNewVersion() ? CQNewFeatureController() : CQWelcomeViewController()
        }
    
    }
    
  /// 判断是否是新版本
    private func isNewVersion() -> Bool {
        // 获取当前的版本号
        let versionString = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String
        let currentVersion = Double(versionString)!
        print("currentVersion: \(currentVersion)")
        
        // 获取到之前的版本号
        let sandboxVersionKey = "sandboxVersionKey"
        let sandboxVersion = NSUserDefaults.standardUserDefaults().doubleForKey(sandboxVersionKey)
        print("sandboxVersion: \(sandboxVersion)")
        
        // 保存当前版本号
        NSUserDefaults.standardUserDefaults().setDouble(currentVersion, forKey: sandboxVersionKey)
        NSUserDefaults.standardUserDefaults().synchronize()
        
        // 对比
        return currentVersion > sandboxVersion
    }

    
    
    
    
    //MARK: - 控制器的x选择
    func switchVC(isMain: Bool) {
        window?.rootViewController = isMain ? CQTabBarController() : CQWelcomeViewController()
    
    }
    
    //MARK: - 渲染函数
    func appreance() {
        UINavigationBar.appearance().tintColor = UIColor.orangeColor()
    }
    
    
}

